var djbh="";	//出库申请单编号
var cgbz="";	//出馆标志
var lsbz="";	//出馆标志
var canreuse = "";
$(document).ready(function(){
	getUserRight();
	$("#functionNav").attr("href",navAddr);
	//获取Url中的参数
	djbh = GetQueryString("djbh");
	cgbz = GetQueryString("cgbz");
	lsbz = GetQueryString("lsbz");
	canreuse = GetQueryString("canreuse");
	//初始化出退库库房Table
	createTableWithOutPage('#ctkKfTable',ProjectName+'/colmanage.getCtkKfList.do','id',ctkKfColTitle,{"djbh":djbh});
})

//锁定验伤
function lockYs(id){
	var sdysJson = ajaxResult("post",false,ProjectName+"/colmanage.ysLock.do",{"id":id});
	if(sdysJson.status=='ok'){
		//alertFunc("锁定验伤成功！");
		$('#ctkKfTable').bootstrapTable('refresh');
	}else{
		alertFunc("锁定验伤失败！");
		return false;
	}
}

//锁定点交
function lockDj(id){
	var sddjJson = ajaxResult("post",false,ProjectName+"/colmanage.djLock.do",{"id":id});
	if(sddjJson.status=='ok'){
		//alertFunc("锁定点交成功！");
		$('#ctkKfTable').bootstrapTable('refresh');
	}else{
		alertFunc("锁定点交失败！");
		return false;
	}	
}

//锁定验伤和点交（不出馆时，责任保管员操作）
function lockYsDj(id){
	lockYs(id);
	lockDj(id);
}

//解锁验伤
function unLockYs(id){
	var jsysJson = ajaxResult("post",false,ProjectName+"/colmanage.ysUnlock.do",{"id":id});
	if(jsysJson.status=='ok'){
		//alertFunc("解锁验伤成功！");
		$('#ctkKfTable').bootstrapTable('refresh');
	}else{
		alertFunc("解锁验伤失败！");
		return false;
	}
}

//解锁点交
function unLockDj(id){
	var jsdjJson = ajaxResult("post",false,ProjectName+"/colmanage.djUnlock.do",{"id":id});
	if(jsdjJson.status=='ok'){
		//alertFunc("解锁点交成功！");
		$('#ctkKfTable').bootstrapTable('refresh');
	}else{
		alertFunc("解锁点交失败！");		
		return false;
	}	
}

//解锁验伤和点交（不出馆时，责任保管员操作）
function unLockYsDj(id){
	unLockYs(id);
	unLockDj(id);
}

var ctkKfColTitle = [{
	 field: 'id',
	 title: '锁定',
	 class:'bs-checkbox',
	 formatter: function(value, row, index) {
		 var result ='';	
		 if(row.wcsl==row.cpzsl)
			 return result;//全部已经完成不能再次锁定
		 if(cgbz==1){//出馆
 			if(userInfo.roleid.indexOf("zerenbaoguanyuan")>-1){	//责任保管员
 				//if(row.ckyssl>0 || row.tkyssl>0){	//出库验伤数、退库验伤数大于0
 					if(row.yssdbz==1){	//已锁定
 						if(row.yssdrid==userInfo.userid){	//锁定人为当前用户，默认选中
 							result = '<input data-index="'+index+'" name="btSelectItem" type="checkbox" value="'+row.id+'" checked="checked" onclick="unLockYs('+row.id+')">';
 						}else{	//锁定人为其他人，不允许锁定
 							result = '<input data-index="'+index+'" name="btSelectItem" type="checkbox" value="'+row.id+'" disabled=true>';
 						}
 					} else{	//未锁定，允许锁定
 						result = '<input data-index="'+index+'" name="btSelectItem" type="checkbox" value="'+row.id+'" onclick="lockYs('+row.id+')">';
 					}	 					
 				//}else{	//出库验伤数、退库验伤数为0，不允许锁定
 				//	result = '<input data-index="'+index+'" name="btSelectItem" type="checkbox" value="'+row.id+'" disabled=true>';
 				//}
 			}else if(userInfo.roleid.indexOf("yanfangjianduyuan")>-1){	//验放监督员
 				//if(row.cksl>0 || row.tksl>0){	//出库数、退库数大于0
 					if(row.djsdbz==1){	//已锁定
 						if(row.djsdrid==userInfo.userid){	//锁定人为当前用户，默认选中
 							result = '<input data-index="'+index+'" name="btSelectItem" type="checkbox" value="'+row.id+'" checked="checked" onclick="unLockDj('+row.id+')">';
 						}else{	//锁定人为其他人，不允许锁定
 							result = '<input data-index="'+index+'" name="btSelectItem" type="checkbox" value="'+row.id+'" disabled=true>';
 						}
 					} else{	//未锁定，允许锁定
 						result = '<input data-index="'+index+'" name="btSelectItem" type="checkbox" value="'+row.id+'" onclick="lockDj('+row.id+')">';
 					}	 					
 				//}else{	//出数、退库数为0，不允许锁定
 				//	result = '<input data-index="'+index+'" name="btSelectItem" type="checkbox" value="'+row.id+'" disabled=true>';
 				//}
 			}
		 }else{	//	不出馆
			 if(userInfo.roleid.indexOf("zerenbaoguanyuan")>-1){	//责任保管员
	 				//if(row.ckyssl>0 || row.tkyssl>0 || row.cksl>0 || row.tksl>0){	//出库验伤数、出库数、退库数、退库验伤数大于0
	 					if(row.yssdbz==1){	//已锁定
	 						if(row.yssdrid==userInfo.userid){	//锁定人为当前用户，默认选中
	 							result = '<input data-index="'+index+'" name="btSelectItem" type="checkbox" value="'+row.id+'" checked="checked" onclick="unLockYsDj('+row.id+')">';
	 						}else{	//锁定人为其他人，不允许锁定
	 							result = '<input data-index="'+index+'" name="btSelectItem" type="checkbox" value="'+row.id+'" disabled=true>';
	 						}
	 					} else{	//未锁定，允许锁定
	 						result = '<input data-index="'+index+'" name="btSelectItem" type="checkbox" value="'+row.id+'" onclick="lockYsDj('+row.id+')">';
	 					}	 					
	 				//}else{	//出库验伤数、出库数、退库数、退库验伤数为0，不允许锁定
	 				//	result = '<input data-index="'+index+'" name="btSelectItem" type="checkbox" value="'+row.id+'" disabled=true>';
	 				//}
	 			}
 		 }	 
   	  	return result;
   	  }
    },{
	        field: 'kfmc',
	        title: '库房名称'
	    }, {
	        field: 'ckyssl',
	        title: '待出库验伤数量'
	    }, {
	        field: 'cksl',
	        title: '待出库数量'
	    }, {
	        field: 'tksl',
	        title: '待退库数量'
	    }, {
	        field: 'tkyssl',
	        title: '待退库验伤数量'
	    },{
	        field: 'bcksl',
	        title: '不出库数量'
	    },{
	        field: 'cpzsl',
	        title: '藏品总数量'
	    },{
	        field: 'wcsl',
	        title: '完成数量'
	    },{
	        field: 'yssdrmc',
	        title: '验伤锁定人',
	        formatter: function(value, row, index) {	
	        	var result="";	        	
    			if(row.yssdbz==1){	//锁定验伤
        			result = value;
        		}        		
		      	return result;
		    }
	    },{
	        field: 'djsdrmc',
	        title: '出退库锁定人',
	        formatter: function(value, row, index) {	
	        	var result="";	        	
    			if(row.djsdbz==1){	//锁定验伤
        			result = value;
        		}        		
		      	return result;
		    }
	    },{
	        field: '',
	        title: '操作',
	        formatter: function(value, row, index) {	
	        	var result="";
        		if(cgbz==1){//出馆
        			if(row.yssdbz==1 && row.yssdrid==userInfo.userid){	//锁定验伤
	        			result += '<a href="ctkInfo.html?djbh='+escape(djbh)+'&kfid='+row.kfid+'&cgbz='+cgbz+'&lsbz='+lsbz+'&canreuse='+canreuse+'">出退库验伤</a>';
	        		}else if(row.djsdbz==1 && row.djsdrid==userInfo.userid){	//锁定点交
	        			result += '<a href="ctkInfo.html?djbh='+escape(djbh)+'&kfid='+row.kfid+'&cgbz='+cgbz+'&lsbz='+lsbz+'&canreuse='+canreuse+'">出退库点交</a>';
	        		}
        		}else{//不出馆
        			if(row.yssdbz==1 && row.yssdrid==userInfo.userid){	//锁定验伤
    					result += '<a href="ctkInfo.html?djbh='+escape(djbh)+'&kfid='+row.kfid+'&cgbz='+cgbz+'&lsbz='+lsbz+'&canreuse='+canreuse+'">出退库验伤和点交</a>';
    				}
        		}
		      	return result;
		    }
	    }]